package Engine.UIControl
{
	import Engine.UIControl.UIBase;
	
	import Interface.UIControl.IIconBase;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.utils.setTimeout;
	
	
	public class IconBase extends UIBase implements IIconBase
	{
		private static var attachMouseIcon:MouseIcon = null;
		private static var DOUBLE_CLICK_TIME:int = 200;
		
		private var _canDrag:Boolean = false;
		private var _canClick:Boolean = false;
		
		
		private var _clickIntval:int = 0;
		private var _ClickTimerIntval:int = 0;
		
		private var _onClickFun:Function = null;
		
		private var _iconBmp:Bitmap = null;
		private var _hasInfo:Boolean = false;
		
		public function IconBase(p_content:Sprite = null,p_width:Number = 42,p_height:Number = 42)
		{
			super(p_content,p_width,p_height);
		}
		
		private function initControl():void
		{
			
		}
		
		override protected function onMouseClick(event:MouseEvent):void
		{
			if(this._hasInfo == false)
				return;
			if(this._canDrag)
			{
				if(this._canClick)
				{
					this._clickIntval++;
					if(!this._ClickTimerIntval)
						this._ClickTimerIntval = setTimeout(onClickBack,DOUBLE_CLICK_TIME);
				}
				else
				{
					this._startDrag();
				}
			}
			else if(this._canClick)
			{
				this._onClickFun();
			}
		}
		
		private function onClickBack():void
		{
			if(this._clickIntval >= 2)
			{
				if(this._canClick)
					this._onClickFun();
			}
			else
				this._startDrag();
			this._clickIntval = 0;
			this._ClickTimerIntval = 0;
		}
		
		private function _startDrag():void
		{
			if(attachMouseIcon == null)
				attachMouseIcon = MouseIcon.getInstance(null,this.width,this.height);
			attachMouseIcon.setParentIcon(this);
			attachMouseIcon.setIconBMP(this._iconBmp.bitmapData);
			attachMouseIcon.onStartDrag(this._content.stage.mouseX,this._content.stage.mouseY);
			attachMouseIcon.show(this._content.stage);
		}
		
		override protected function onMouseDown(event:MouseEvent):void
		{
			
		}
		
		override protected function onMouseUp(event:MouseEvent):void
		{
			
		}
		
		override protected function onMouseIn(event:MouseEvent):void
		{
			
		}
		
		override protected function onMouseOut(event:MouseEvent):void
		{
			
		}
		
		public function setDrag(drag:Boolean):void
		{
			this._canDrag = drag;
		}
		
		protected function _setClickFun(fun:Function):void
		{
			if(fun == null)
			{
				this._onClickFun = null;
				this._canClick = false;
			}
			else
			{
				this._onClickFun = fun;
				this._canClick = true;
			}
		}
		
		public function onDropEvent(p_dropTarget:DisplayObject):void
		{
			if(p_dropTarget == null || p_dropTarget.parent == null || p_dropTarget.parent == this._content)
				return;
			if(p_dropTarget.parent is IconBase)
				(p_dropTarget.parent as IconBase).setIconBMP(this._iconBmp.bitmapData);
		}
		
		public function setIconBMP(btData:BitmapData):void
		{
			this._iconBmp = new Bitmap(btData);
			this._iconBmp.width = this.width;
			this._iconBmp.height = this.height;
			this._content.addChild(this._iconBmp);	
			this._hasInfo = true;
		}
		
		public function removeIconBMP():void
		{
			if(this._iconBmp != null)
				this._content.removeChild(this._iconBmp);
			this._iconBmp = null;
			this._hasInfo = false;
		}
		
		public function setSize(width:Number,height:Number):void
		{
			
		}
	}
}